#include "ContactsMapper.h"
#include "../common/ContactException.h"
#include<fstream>
const static std::string FILE_NAME = "contacts.bin";
using std::ios;
using std::cout;
using std::endl;

void ContactsMapper::insertContacts(contacts::Contacts& contacts) const
{
   std::fstream output(FILE_NAME,ios::out|ios::binary|ios::trunc);
   if(!contacts.SerializeToOstream(&output))
   {
        output.close();
        throw ContactExpection("(ContactsMapper::insertContacts) Failed to writer contacts.");
   }
   output.close();
}
 void ContactsMapper::selectContacts(contacts::Contacts* contacts) const
 {
    std::fstream input(FILE_NAME.c_str(),ios::in|ios::binary);
    if(!input)
    {
        cout<<"打开文件失败"<<endl;
    }
    else if(!contacts->ParseFromIstream(&input))
    {
        input.close();
        throw ContactExpection("(ContactsMapper::selectContacts) Failed to parse contacts.");
    }
    input.close();
 }


